<?php


namespace app\backend\controller\v1;

use app\backend\validate\InformationValidate;
use app\common\model\Information as InformationModel;
use think\exception\ValidateException;
use think\facade\Filesystem;

class Information extends BaseController
{
    /**
     * 网站基础设置
     *
     * @return \think\Response
     */
    public function index()
    {
        $user = new InformationModel();
        // where条件
        $where = [];
        // 分页、排序等
        $page = input('page/d', 1);
        $pageSize = input('pageSize/d', 10);
        $sort = ['sort' => 'asc'];
        $field = '*';
        //获取列表
        $list = $user->fetchListByWhere($where, $sort, $field, $page, $pageSize);
        foreach ($list as &$v) {
            $v['logo'] = config("app.domain_url") . str_replace('\\', '/', $v['logo']);
            $v['image'] = config("app.domain_url") . str_replace('\\', '/', $v['image']);
        }
        return sucessMsg(200, '获取成功！', ['list' => $list, 'total' => count($list)]);
    }

    /**
     * 添加
     *
     * @return \think\Response
     */
    public function create()
    {
        $user = new InformationModel();
        $data = input();
        $img1 = $file1 = request()->file('logo');
        $img2 = $file2 = request()->file('image');
        try {
            validate(InformationValidate::class)->scene('create')->check($data);
            if (empty($img1)) {
                return errMsg(422, '请上传logo!');
            }
            if (empty($img2)) {
                return errMsg(422, '请上传图片!');
            }
            $saveName1 = Filesystem::disk('public')->putFile('/uploads', $file1);
            $data['logo'] = $saveName1;
            $data['logo'] = str_replace('\\', '/', $data['logo']);
            $saveName2 = Filesystem::disk('public')->putFile('/uploads', $file2);
            $data['image'] = $saveName2;
            $data['image'] = str_replace('\\', '/', $data['image']);
            if ($user->save($data)) {
                return sucessMsg(200, '创建成功！');
            }
            return errMsg(500, '创建失败！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }


    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update()
    {
        $id = input('id/d');
        $data = input();
        $img1 = $file1 = request()->file('logo');
        $img2 = $file2 = request()->file('image');
        if (empty($id)) {
            return errMsg(400, '请选择数据！');
        }
        $user = InformationModel::find($id);
        if (empty($user)) {
            return errMsg(400, '要删除的数据为空！');
        }
        try {
            validate(InformationValidate::class)->scene('update')->check($data);
            if (!empty($img1)) {
                $saveName = Filesystem::disk('public')->putFile('/uploads', $file1);
                $data['logo'] = $saveName;
                $data['logo'] = str_replace('\\', '/', $data['logo']);
            }
            if (!empty($img2)) {
                $saveName = Filesystem::disk('public')->putFile('/uploads', $file2);
                $data['image'] = $saveName;
                $data['image'] = str_replace('\\', '/', $data['image']);
            }
<<<<<<< HEAD
            $saveName1 = Filesystem::disk('public')->putFile('/uploads', $file1);
            $saveName2 = Filesystem::disk('public')->putFile('/uploads', $file2);
            $data['logo'] = str_replace('\\', '/',$saveName1);
            $data['image'] = str_replace('\\', '/',$saveName2);
=======
>>>>>>> 3863eb4f2abfc8a080e4a08c0e25a3633e28cc5a
            if (InformationModel::update($data, ['id'=>$id])) {
                return sucessMsg(200, '修改成功！');
            }
            return errMsg(500, '修改失败！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete()
    {
        $id = input('id/d', 0);
        if (empty($id)) {
            return errMsg(400, '请选择要删除数据的id！');
        }
        $data = InformationModel::find($id);
        if (empty($data)){
            return errMsg(400, '要删除的数据为空！');
        }
        if (InformationModel::destroy($id)) {
            return sucessMsg(200, '删除成功！');
        }
        return errMsg(500, '删除失败！');
    }
}